<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/WEB-INF/vectrics.tld"    prefix="vectrics" %>


<vectrics:errors/>

<%@ page import="org.vectrics.hibernate.*" %>

<%
	String locationId = request.getParameter("locationId");
	String selectParam = request.getParameter("selectParam");
	
	String userName = ThreadProperties.getPerson().getUser().getUserName();
	String password = ThreadProperties.getPerson().getUser().getPassword();
	String domainId = ThreadProperties.getDomain().getId().toString();
	String contextPath = request.getContextPath();
%>



<script>
<!--
	var currentPersonNodes;

	function changeColor(element, to) {
		log('changeColor called');
		element.style.background = to;
	};

	 function findMatches() { 
	 	log('findMatches() called');
	    var locationEdit = document.getElementById("edit_locationName");
		var location = locationEdit.value;

		var href = '<%=contextPath%>/ws?command=get.matching.locations&user=<%=userName%>&domainId=<%=domainId%>&pcode=<%=password%>&domainId=1&name=';
		href += location;
	 	log('findMatches() - href: ' + href);
		remoteCall(href, handleXmlSearchResponse);
	 } 

	function findMatchesRemoteCall(lastName) {
		
	}

	function selectItem(id, locationName) {
		log('Selected Item: ' + id);
		log('Select Code: <%=selectParam%>');
	  	opener.receiveLocationPick(id, locationName, '<%=selectParam%>');
		window.close();	
	}

	



 	function handleXmlSearchResponse() {
			  // if the readyState code is 4 (Completed)
			  // and http status is 200 (OK) we go ahead and get the responseText
			  // other readyState codes:
			  // 0=Uninitialised 1=Loading 2=Loaded 3=Interactive
  			  log("Response received from server, processing XML response.");
			  if (xmlhttp.readyState == 4) {
					if (xmlhttp.status == 200) {
							xmlDoc =
								AjxXmlDoc.createFromXml(xmlhttp.responseText);

							nativeDocument = xmlDoc.getDoc();
							currentPersonNodes = nativeDocument.getElementsByTagName("location");
							displayReturnedNodes(currentPersonNodes);
					} else {
	  			  		log("Not processed - Response status = " + xmlhttp.status);
					}
			  } else {
  			  		log("Not processed - Response readyState = " + xmlhttp.readyState);
			  }
	}


	function displayReturnedNodes(currentPersonNodes) {
		var resultsTable = '<table width="100%" style="cursor: pointer" id="location.select.table">';
		resultsTable += '<tr>'
			+ '<td><font class="subFont"><b>Id</b></font></td>'
			+ '<td><font class="subFont"><b>Name</b></font></td>'
			+ '<td><font class="subFont"><b>Type</b></font></td>'
			+ '</tr>';
		var normalRowColor = '#eeee88';
		var selectRowColor = '#cccccc';
		var quotedNormalRowColor = "'" + normalRowColor + "'";
		var quotedSelectRowColor = "'" + selectRowColor + "'";
		for (i = 0; i < currentPersonNodes.length; i++) {
			node = currentPersonNodes[i];
			var quotedId = "'" + node.getAttribute("id") + "'";
			var fontIdId = 'font.loc.id.' + node.getAttribute("id");
			var fontIdName = 'font.loc.name.' + node.getAttribute("id");
			var fontIdType = 'font.loc.type.' + node.getAttribute("id");

			var cellIdId = 'font.loc.id.' + node.getAttribute("id");
			var cellIdName = 'font.loc.name.' + node.getAttribute("id");
			var cellIdType = 'font.loc.type.' + node.getAttribute("id");
			
			var quotedName = "'" + node.getAttribute("name") + "'";
			log('quotedId=' + quotedId);
			var trStr;
			trStr = '<tr'; 
			trStr += ' id="loc.' + node.getAttribute("id") + '" ';
			trStr += ' onClick="selectItem(' + quotedId + ', ' + quotedName + ');"';
			trStr += ' onMouseOver="changeColor(this, '	+ quotedSelectRowColor + ');"';
			trStr += ' onMouseOut="changeColor(this, '	+ quotedNormalRowColor + ');"';
			trStr += '>';
			log(trStr);
			
			var detailStr = trStr
				+ '<td id="' + cellIdId + '"><font class="subFont" id="' + fontIdId + '">' + node.getAttribute("id") + '</font></td>'
				+ '<td id="' + cellIdName + '"><font class="subFont" id="' + fontIdName + '">' + node.getAttribute("name") + '</font></td>'
				+ '<td id="' + cellIdType + '"><font class="subFont" id="' + fontIdType + '">' + node.getAttribute("typeCode") + '</font></td>'
				+ '</tr>';
			resultsTable += detailStr;
		}
		resultsTable += '</table>';
		log('Setting table to new contents');
	    document.getElementById("search_results").innerHTML = resultsTable;
		log('Done - Setting table to new contents');
	}

 -->
</script>


<table>
	<tr>
		<td>
			<vectrics:locationSearchForm locationId="<%=locationId%>"/>
		</td>
	</tr>
	<tr>
		<td>
			<div id="search_results" style="position:relative; top:10px; left:10px; width:90%; height:300px;
				background-color:#dddda0; text-align:left; padding:0px;border: solid 1px #339999;
				z-index:0;overflow:auto;">
			</div>
		</td>
	</tr>
</table>

